//
//  SearchVC.m
//  垃圾管理者
//
//  Created by Steven on 2019/7/25.
//  Copyright © 2019 Steven. All rights reserved.
//

#import "SearchVC.h"
#import "AppCenter.h"
#import "NSArray+Util.h"
@interface SearchVC () <UISearchControllerDelegate, UISearchResultsUpdating>
@property(nonatomic, strong)NSArray *dataArray;
@property(nonatomic, strong)NSMutableArray *searchList;
@property(nonatomic, strong)UISearchController *searchController;
@end

@implementation SearchVC

- (NSArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)searchList{
    if (_searchList == nil) {
        _searchList = [NSMutableArray array];
    }
    return _searchList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = appData.homeDatas;
    
    //创建UISearchController
    
    self.searchController= [[UISearchController alloc]initWithSearchResultsController:nil];
    
    //设置代理
    
    self.searchController.delegate = self;
    
    self.searchController.searchResultsUpdater = self;
    
    //设置UISearchController的显示属性，以下3个属性默认为YES
    
    //搜索时，背景变暗色
    
    self.searchController.dimsBackgroundDuringPresentation = NO;
    
    //搜索时，背景变模糊
    
    self.searchController.obscuresBackgroundDuringPresentation = NO;
    
    //隐藏导航栏
    
    self.searchController.hidesNavigationBarDuringPresentation = YES;
    
    self.searchController.searchBar.frame = CGRectMake(0,0,self.searchController.searchBar.frame.size.width,44.0);
    
    self.searchController.searchBar.placeholder = @"请输入要查询的东西";
    self.searchController.searchBar.backgroundColor = [UIColor whiteColor];

    //添加到searchBar到tableView的header
    self.tableView.tableHeaderView = self.searchController.searchBar;
    
    
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.searchController.active) {
        return [self.searchList count];
    }else{
        return [self.dataArray count];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSArray *titleArray = @[@"湿垃圾", @"干垃圾", @"可回收物", @"有害垃圾"];
    NSDictionary *data = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchCell" forIndexPath:indexPath];
    if (self.searchController.isActive) {
        cell.textLabel.text = self.searchList[indexPath.row];
    }else{
      cell.textLabel.text = data[@"name"];
    }
    
    if (!self.searchController.isActive) {
        cell.detailTextLabel.text = [NSString stringWithFormat:@"【%@】", titleArray[[data[@"cats"] integerValue]]];
    }else{
        cell.detailTextLabel.text = @"";
    }
    return cell;
}

#pragma mark 更新表格的显示结果

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSString *searchString = [self.searchController.searchBar text];
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    if (self.searchList!= nil) {
        [self.searchList removeAllObjects];
    }
    
    NSArray *nameArray = [[self.dataArray mutableCopy] map:^NSString *(NSDictionary *item) {
        return item[@"name"];
    }];
    //过滤数据
    self.searchList = [NSMutableArray arrayWithArray:[nameArray filteredArrayUsingPredicate:preicate]];
    //刷新表格
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSArray *titleArray = @[@"湿垃圾", @"干垃圾", @"可回收物", @"有害垃圾"];
    if (self.searchController.isActive) {
        
        NSInteger index = [self.dataArray find:^BOOL(NSDictionary *item) {
            return [item[@"name"] isEqualToString:self.searchList[indexPath.row]];
        }];
        
        NSString *type = titleArray[[self.dataArray[index][@"cats"] integerValue]];
        
        NSString *message = [NSString stringWithFormat:@"%@ 【%@】",self.searchList[indexPath.row],type];
        
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:nil]];
        
        [self presentViewController:alert animated:YES completion:nil];
        
        
    }
    
}

@end
